package com.chexiehui.jc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.event.ChooseBrandEvent;
import com.chexiehui.jc.ui.fragment.ChooseCarModelBrandFragment;
import com.chexiehui.jc.ui.fragment.ChooseCarModelCarFragment;
import com.chexiehui.jc.ui.fragment.ChooseCarModelSeriesFragment;
import com.chexiehui.jc.utils.NoDoubleClickListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择品牌 车系 车型
 */
public class ChooseCarModelActivity extends BaseActivity implements
        ChooseCarModelBrandFragment.OnBrandChooseListener, ChooseCarModelSeriesFragment.OnSeriesChooseListener,
        ChooseCarModelCarFragment.OnCarChooseListener {

    @BindView(R.id.fl_choose_page)
    FrameLayout flChoosePage;

    private BrandInfo brandInfo;
    private CarSeriesInfo carSeriesInfo;
    private CarInfo carInfo;
    private int chooseLevel = 3;
    private int eventTag;
    private boolean isFastChoose;
    private int chooseCarTitle = 0;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
            chooseLevel = bundle.getInt("chooseLevel", 3);
            eventTag = bundle.getInt("eventTag", -1);
            //brandInfo 传递过来 说明直接从车系开始选
            brandInfo = (BrandInfo) bundle.getSerializable("brandInfo");
            // seriesInfo 传递过来 说明直接选车辆
            carSeriesInfo = (CarSeriesInfo) bundle.getSerializable("seriesInfo");
        }
        if(brandInfo != null || carSeriesInfo != null){
            isFastChoose = true;
        }
    }

    @Override
    protected void setTitleBar() {
        titleBar.setOnLeftTextClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if(isFastChoose){
                    if(carSeriesInfo != null && chooseCarTitle == 3){
                        finish();
                        return;
                    }else if(brandInfo != null && chooseCarTitle == 3){
                        transactionToSeries();
                        return;
                    }else if(brandInfo != null && chooseCarTitle == 2){
                        finish();
                        return;
                    }
                }
                if (chooseCarTitle == 1){
                    finish();
                }else if (chooseCarTitle == 2){
                    transactionToBrand();
                }else if (chooseCarTitle == 3){
                    transactionToSeries();
                }
            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_choose_car_model;
    }

    @Override
    protected void initView(Bundle var1) {
        if(brandInfo != null){
            transactionToSeries();
        }else if(carSeriesInfo != null){
            transactionToCar();
        }else{
            transactionToBrand();
        }
    }

    private void transactionToBrand(){
        chooseCarTitle = 1;
        titleBar.setTitleMainText("品牌");
        ChooseCarModelBrandFragment fragment = ChooseCarModelBrandFragment.getInstance();
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    private void transactionToSeries(){
        chooseCarTitle = 2;
        titleBar.setTitleMainText("车系");
        ChooseCarModelSeriesFragment fragment = ChooseCarModelSeriesFragment.getInstance(this.brandInfo);
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    private void transactionToCar(){
        chooseCarTitle = 3;
        titleBar.setTitleMainText("选择车型");
        ChooseCarModelCarFragment fragment = ChooseCarModelCarFragment.getInstance(this.carSeriesInfo);
        fragment.setOnChooseListener(this);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_choose_page, fragment);
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (chooseCarTitle != 0){
            chooseCarTitle = 0;
        }
    }

    @Override
    public void onBrandChoose(BrandInfo brandInfo) {
        this.brandInfo = brandInfo;
        if(chooseLevel > 1){
            transactionToSeries();
        }else{
            if(eventTag > -1){
                EventBus.getDefault().post(new ChooseBrandEvent(brandInfo, eventTag));
            }
            Intent data = new Intent();
            data.putExtra("brandInfo", brandInfo);
            setResult(RESULT_OK, data);
            finish();
        }
    }

    @Override
    public void onSeriesChoose(CarSeriesInfo carSeriesInfo) {
        this.carSeriesInfo = carSeriesInfo;
        if(chooseLevel > 2){
            transactionToCar();
        }else{
            Intent data = new Intent();
            data.putExtra("brandInfo", brandInfo);
            data.putExtra("seriesInfo", carSeriesInfo);
            setResult(RESULT_OK, data);
            finish();
        }

    }

    @Override
    public void onCarChoose(CarInfo carInfo) {
        this.carInfo = carInfo;
        Intent data = new Intent();
        data.putExtra("brandInfo", brandInfo);
        data.putExtra("seriesInfo", carSeriesInfo);
        data.putExtra("carInfo", carInfo);
        setResult(RESULT_OK, data);
        finish();
    }
}
